<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Sub_FamilyController
 *
 * @author julien
 */
require_once 'Classes/Controllers/Controller.php';
require_once 'Classes/Model/Sub_FamilyModel.php';

class Sub_FamiliesController extends Controller {

    
    public function processRequest() {
        switch (HTTPRequestModel::getInstance()->extractGETValue('action')){
            case 'AddSubFamily':
                $this->addSub_Family();
                break;
            case 'EditSubFamily':
                $this->editSub_Family();
                break;
            case 'DeleteSubFamily':
                $this->deleteSub_Family();
                break;
            case 'ListAllSubFamilies':
                $this->listAllSubfamilies();
                break;
            default:
                $this->listSub_Families();
                break;
        }
    }
    
    
    public function listAllSubfamilies(){
       require_once 'Classes/View/Pages/ListBasedPage.php';

        $Sub_Families = Sub_FamilyModel::getInstance()->ListAllSub_Families();
        $page = new ListBasedPage('Subfamilies');
        $page->addCSS('/Ressources/style.css');
        $page->addElement($Sub_Families);
        $page->display();
    }




    public function addSub_Family(){  
        
        
        
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
             
            $name= HTTPRequestModel::getInstance()->extractPOSTValue('Title');
            $optional_1= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_1');
            $optional_2= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_2');
            $optional_3= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_3');
            $optional_4= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_4');
            $optional_5= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_5');
            $optional_6= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_6');
           
            $family= HTTPRequestModel::getInstance()->extractGETValue('family');
           // $sub_Family = new sub_Family("", $name, $optional_1, $optional_2, $optional_3, $optional_4, $optional_5, $optional_6);
           // $sub_Family->setFamilyID($family);
            $error=0;
            if($name =="" OR is_null($name)){
              
                $error++;
            }
            
            if($error == 0){
                Sub_FamilyModel::getInstance()->AddSub_Family($name, $optional_1, $optional_2, $optional_3, $optional_4, $optional_5, $optional_6,$family);
                header('Location: /'.$family.'/');
                
            }
            else {
                
                require_once 'Classes/View/Pages/FormBasedPage.php';
                
                $this->view = new FormBasedPage("New Sub Family");
                $this->view->addCSS('/Ressources/style.css');
                $familyId = HTTPRequestModel::getInstance()->extractGETValue('family');
                $this->view->addElement($subfamily);
                $this->view->display();
                
            }
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';

            $this->view = new FormBasedPage("New Sub Family");
            $this->view->addCSS('/Ressources/style.css');
            $this->view->addElement(new Sub_Family());
            $this->view->display();
         }       
    }
    
    public function deleteSub_Family(){
        

        if ( strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get"){

            $id= HTTPRequestModel::getInstance()->extractGETValue('subfamily');
            $family= HTTPRequestModel::getInstance()->extractGETValue('family');

            Sub_FamilyModel::getInstance()->DeleteSub_FamilyById($id);
            header('Location: /'.$family);
            
        }
    }
    
    public function editSub_Family(){        
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
            $name= HTTPRequestModel::getInstance()->extractPOSTValue('Title');
            $optional_1= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_1');
            $optional_2= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_2');
            $optional_3= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_3');
            $optional_4= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_4');
            $optional_5= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_5');
            $optional_6= HTTPRequestModel::getInstance()->extractPOSTValue('Optional_6');
            $id= HTTPRequestModel::getInstance()->extractGETValue('subfamily');
            $family= HTTPRequestModel::getInstance()->extractGETValue('family');
            $subfamily = new Sub_Family($id,$name, $optional_1, $optional_2, $optional_3, $optional_4, $optional_5, $optional_6,$family);
            
            $error=0;
            if($name =="" OR is_null($name)){
                $error++;
            }
            
            if($error == 0){
                Sub_FamilyModel::getInstance()->UpdateSub_FamilyById($id,$name, $optional_1, $optional_2, $optional_3, $optional_4, $optional_5, $optional_6);
                header('Location: /'.$family.'/');
             }
            else {
                
                require_once 'Classes/View/Pages/FormBasedPage.php';
                
                $this->view = new FormBasedPage("Edit Sub Family");
                $this->view->addCSS('/Ressources/style.css');
                $familyId = HTTPRequestModel::getInstance()->extractGETValue('family');
                $this->view->addElement($subfamily);
                $this->view->display();
                
            }
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';
            $id= HTTPRequestModel::getInstance()->extractGETValue('subfamily');

            $subfamily = new Sub_Family(Sub_FamilyModel::getInstance()->ListSub_FamiliesById($id));
            
            $this->view = new FormBasedPage("Edit Sub Family");
            $this->view->addCSS('/Ressources/style.css');
            $this->view->addElement($subfamily);
            $this->view->display();
         }  
    }
    
    public function listSub_Families(){
        require_once 'Classes/View/Pages/ListBasedPage.php';

        if ( strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get"){
            
            $title = HTTPRequestModel::getInstance()->extractGETValue("family");
            try{
                $Sub_Families = Sub_FamilyModel::getInstance()->ListSub_FamiliesByFamilyId($title);
            }
            catch(NoObjectInDatabaseException $e){
                $Sub_Families=null;
            }
            $page = new ListBasedPage('Sub families');
            $page->addCSS('/Ressources/style.css');
            $page->addElement($Sub_Families);
            $page->display();
        }
       
        
    }
    
}

?>
